push(@INC, "..");
push(@INC, "../Managers");
push(@INC, "../MapLoaders");

## ========================================================
## ========================================================
## Common helpful routines and data structures for tests
## ========================================================
## ========================================================

# ==========================================================
# Logging support:
# ================
#
# Simple logging support, mainly a stub to add better, more
# complete functionality if it's needed at some point

# Parameters: Message
# For now, simply prints the message to the screen and ends current the line
# Apparently, "log" as a function name is reserved
sub printLog{
    my $msg = shift;
    print $msg;
    print "\n";
}


# ==========================================================
# Assertion Results
# =================
#
# Key   : A description of whatever was asserted 
# Value : Result of the assertion
#         0 - Failure
#         1 - Success
%assertResults = ();

# ==========================================================
# Assertion subroutines: 
# ======================
#
# Parameters are: 
#                 Asserted value
#                 Expected value
#                 Short, unique description of the assertion (it is used a hash key)
#                 Message when assertion passes
#                 Message when assertion fails 


# Not Numerical tests -----------------------------
# -------------------------------------------------

# Params: value, expected, description
# The test performed is: value eq expected
sub assertEq{
    my ($value, $expected, $desc) = (shift, shift, shift);
    if($value eq $expected){
	$assertResults{$desc} = 1;
	printLog("--->OK: ".$desc);
    }else{
	$assertResults{$desc} = 0;
	printLog("--->ERROR: ".$desc);
    }
}

# Params: value, expected, description
# The test performed is: value ne expected
sub assertNEq{
    my ($value, $expected, $desc) = (shift, shift, shift);
    if($value ne $expected){
	$assertResults{$desc} = 1;
	printLog("--->OK: ".$desc);
    }else{
	$assertResults{$desc} = 0;
	printLog("--->ERROR: ".$desc);
    }
}

# Numerical tests ---------------------------------
# -------------------------------------------------

# Params: value, expected, description
# The test performed is: value == expected
sub assertNumericalEq{
    my ($value, $expected, $desc) = (shift, shift, shift);
    if($value == $expected){
	$assertResults{$desc} = 1;
	printLog("--->OK: ".$desc);
    }else{
	$assertResults{$desc} = 0;
	printLog("--->ERROR: ".$desc);
    }
}

# Params: value, expected, description
# The test performed is: value != expected
sub assertNumericalNEq{
    my ($value, $expected, $desc) = (shift, shift, shift);
    if($value != $expected){
	$assertResults{$desc} = 1;
	printLog("--->OK: ".$desc);
    }else{
	$assertResults{$desc} = 0;
	printLog("--->ERROR: ".$desc);
    }
}

# Shortcuts  --------------------------------------
# -------------------------------------------------

# Params: value, description
# Shortcut to assertNumericalEq($value, 1, $variable_name);
sub assertTrue{
    my ($value, $desc) = (shift, shift, shift);
    assertNumericalEq($value, 1, $desc);
}

# Params: value, description
# Shortcut to assertNumericalNEq($value, 1, $variable_name);
sub assertTrue{
    my ($value, $desc) = (shift, shift, shift);
    assertNumericalNEq($value, 1, $desc);
}


# ==========================================================
# Printing assertion results: 
# ===========================
# 
# Prints each assertion performed and the result


# For all asserts recorded, prints description and result
sub printAssertResults{
	my @assertDescriptions = keys %assertResults;

    	print "\n";
	print "FINAL REPORT:\n";
	print "=============\n";
	foreach my $assertDesc (@assertDescriptions){
	    my $result = $assertResults{$assertDesc};

	    if($result){
		print "    OK:    ";
	    }else{
		print "--->ERROR: ";
	    }   

	    print $assertDesc;
	    print " FAILED" unless($result);
	    print "\n";
	} #foreach(assertion)
}


1;
